package com.atguigu.gmall.cart.rpc;

import com.atguigu.gmall.cart.entity.CartInfo;
import com.atguigu.gmall.cart.service.CartInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuInfo;
import feign.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lhh
 * @ClassName CartRpcController
 * @date 2022 2022/12/18
 */
@RequestMapping("/api/inner/rpc/cart")
@RestController
public class CartRpcController {

    @Autowired
    CartInfoService cartInfoService;

    /**
     * 把商品添加到购物车
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/add/{skuId}/{num}")
    public Result<SkuInfo> addToCart(@PathVariable("skuId") Long skuId,
                                     @PathVariable("num") Integer num,
                                     Request.Options options){

        //1、用哪个购物车
        String cartKey = cartInfoService.determinCartKey();
        //2、把指定商品添加到指定购物车
        SkuInfo skuInfo = cartInfoService.addToCart(skuId,num,cartKey);
        return Result.ok(skuInfo);
    }

    /**
     * 删除购物车选中的商品
     * @return
     */
    @GetMapping("deleteChecked")
    public Result deleteChecked(){
        String cartKey = cartInfoService.determinCartKey();
        cartInfoService.deleteChecked(cartKey);
        return Result.ok();
    }

    /**
     * 获取选中的商品
     * @return
     */
    @GetMapping("/getChecked")
    public Result<List<CartInfo>> getChecked(){
        String cartKey = cartInfoService.determinCartKey();
        List<CartInfo> cartInfos = cartInfoService.getCheckeds(cartKey);
        return Result.ok(cartInfos);
    }
}
